<!DOCTYPE html><html lang="zh-Hans"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"><meta name="description" content="vue nuxt node部署服务器"><meta name="keywords" content="node 服务器 部署"><meta name="author" content="晟松"><meta name="copyright" content="晟松"><title>vue nuxt node部署服务器 | 晟松</title><link rel="shortcut icon" href="/logo.png"><link rel="stylesheet" href="/css/index.css?version=1.9.0"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome@latest/css/font-awesome.min.css?version=1.9.0"><meta name="format-detection" content="telephone=no"><meta http-equiv="x-dns-prefetch-control" content="on"><link rel="dns-prefetch" href="https://cdn.jsdelivr.net"><link rel="dns-prefetch" href="https://hm.baidu.com"><script>var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?c34d81d53341febe06a155941e2df16d";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();</script><meta http-equiv="Cache-Control" content="no-transform"><meta http-equiv="Cache-Control" content="no-siteapp"><script>var GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"search.xml","languages":{"hits_empty":"找不到您查询的内容:${query}"}},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  hexoVersion: '5.4.0'
} </script><meta name="generator" content="Hexo 5.4.0"></head><body><i class="fa fa-arrow-right" id="toggle-sidebar" aria-hidden="true"></i><div id="sidebar" data-display="true"><div class="toggle-sidebar-info text-center"><span data-toggle="切换文章详情">切换站点概览</span><hr></div><div class="sidebar-toc"><div class="sidebar-toc__title">目录</div><div class="sidebar-toc__progress"><span class="progress-notice">你已经读了</span><span class="progress-num">0</span><span class="progress-percentage">%</span><div class="sidebar-toc__progress-bar"></div></div><div class="sidebar-toc__content"><ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#vue"><span class="toc-number">1.</span> <span class="toc-text">vue</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#nuxt"><span class="toc-number">2.</span> <span class="toc-text">nuxt</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%9C%8D%E5%8A%A1%E5%99%A8%E6%B8%B2%E6%9F%93%E5%BA%94%E7%94%A8%E9%83%A8%E7%BD%B2"><span class="toc-number">2.1.</span> <span class="toc-text">服务器渲染应用部署</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%9D%99%E6%80%81%E5%BA%94%E7%94%A8%E9%83%A8%E7%BD%B2"><span class="toc-number">2.2.</span> <span class="toc-text">静态应用部署</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#node"><span class="toc-number">3.</span> <span class="toc-text">node</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%9F%9F%E5%90%8D%E7%9B%B8%E5%85%B3"><span class="toc-number">4.</span> <span class="toc-text">域名相关</span></a></li></ol></div></div><div class="author-info hide"><div class="author-info__avatar text-center"><img src="/img/avatar.png"></div><div class="author-info__name text-center">晟松</div><div class="author-info__description text-center">晟松的博客</div><div class="follow-button"><a target="_blank" rel="noopener" href="https://gitee.com/cheng_song">Gitee</a></div><hr><div class="author-info-articles"><a class="author-info-articles__archives article-meta" href="/archives"><span class="pull-left">文章</span><span class="pull-right">52</span></a><a class="author-info-articles__tags article-meta" href="/tags"><span class="pull-left">标签</span><span class="pull-right">45</span></a></div></div></div><div id="content-outer"><div id="top-container" style="background-image: url(/img/blog-bg.jpg)"><div id="page-header"><span class="pull-left"> <a id="site-name" href="/">晟松</a></span><i class="fa fa-bars toggle-menu pull-right" aria-hidden="true"></i><span class="pull-right menus">   <a class="site-page" href="/">Home</a><a class="site-page" href="/archives">Archives</a><a class="site-page" href="/tags">Tags</a><a class="site-page" href="/categories">Categories</a></span><span class="pull-right"><a class="site-page social-icon search"><i class="fa fa-search"></i><span> 搜索</span></a></span></div><div id="post-info"><div id="post-title">vue nuxt node部署服务器</div><div id="post-meta"><time class="post-meta__date"><i class="fa fa-calendar" aria-hidden="true"></i> 2020-11-28</time><div class="post-meta-wordcount"><span>字数总计: </span><span class="word-count">1.5k</span><span class="post-meta__separator">|</span><span>阅读时长: 5 分钟</span></div></div></div></div><div class="layout" id="content-inner"><article id="post"><div class="article-container" id="post-content"><p>就以一个对自己的vue和node项目关于服务器部署的各种各样的问题的记录和解决</p>
<p>首先对于一些先决条件，比如服务器的购买和使用宝塔面板管理服务器，安全组，端口这些，就不记录了</p>
<p>连接服务器（例子）：ssh <a href="mailto:&#114;&#x6f;&#111;&#x74;&#x40;&#x31;&#x37;&#50;&#x2e;&#50;&#x30;&#x2e;&#x30;&#x2e;&#x38;&#48;">&#114;&#x6f;&#111;&#x74;&#x40;&#x31;&#x37;&#50;&#x2e;&#50;&#x30;&#x2e;&#x30;&#x2e;&#x38;&#48;</a>（ip地址）</p>
<p>输入172.20.0.80的密码即可连接到服务器。</p>
<p><a target="_blank" rel="noopener" href="https://www.pianshen.com/article/27801079363/">vue+node+mysql参考</a></p>
<h3 id="vue"><a href="#vue" class="headerlink" title="vue"></a>vue</h3><p><strong>跨域</strong>：一般在开发环境下，对于远程的http请求可直接使用vue.config.js里面使用proxy代理一下，处理跨域问题</p>
<p><a target="_blank" rel="noopener" href="https://www.cnblogs.com/bobo1/p/12588079.html">简单proxy讲解</a></p>
<p>但如果后端没有对跨域进行处理的话，部署到服务器之后，会有问题，如在本地是对<code>axios.get(&#39;/api/post&#39;,params:&#123;&#125;)</code>，本地请求会变成</p>
<p><code>http://115.159.103.48/post?id=1</code>，得到正确的数据</p>
<p>但部署之后会变成<code>http://myui.chengsong.info/api/post?id=1</code>，请求失败</p>
<p>这里的 myui.chengsong.info 在本次开发时显示的是 localhost:8080</p>
<p>可以对axios封装的时候，可使用</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">axios.defaults.baseURL=<span class="string">&#x27;后端ip地址&#x27;</span>	<span class="comment">//例：http://47.99.134.126:28019/api</span></span><br></pre></td></tr></table></figure>

<p>后面再进行axios请求的时候，部署到服务器就不会有这些问题了</p>
<p>其实更好的是在后端直接处理跨域，然后就不用代理这个东西了</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> app = express();</span><br><span class="line"><span class="comment">//设置跨域访问</span></span><br><span class="line">app.all(<span class="string">&#x27;*&#x27;</span>, <span class="function"><span class="keyword">function</span>(<span class="params">req, response, next</span>) </span>&#123;</span><br><span class="line"><span class="comment">//设置允许跨域的域名，*代表允许任意域名跨域</span></span><br><span class="line">response.header(<span class="string">&quot;Access-Control-Allow-Origin&quot;</span>, <span class="string">&quot;*&quot;</span>);</span><br><span class="line"><span class="comment">//允许的header类型</span></span><br><span class="line">response.header(<span class="string">&quot;Access-Control-Allow-Headers&quot;</span>, <span class="string">&quot;X-Requested-With&quot;</span>);</span><br><span class="line"><span class="comment">//跨域允许的请求方式</span></span><br><span class="line">response.header(<span class="string">&quot;Access-Control-Allow-Methods&quot;</span>,<span class="string">&quot;PUT,POST,GET,DELETE,OPTIONS&quot;</span>);</span><br><span class="line"><span class="comment">//设置响应头信息</span></span><br><span class="line">response.header(<span class="string">&quot;X-Powered-By&quot;</span>,<span class="string">&#x27; 3.2.1&#x27;</span>)</span><br><span class="line">response.header(<span class="string">&quot;Content-Type&quot;</span>, <span class="string">&quot;application/json;charset=utf-8&quot;</span>);</span><br><span class="line">next();</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>

<p>还有一些其他的更具体的对允许跨域的域名的控制，需要的时候，再进行处理</p>
<p><strong>部署</strong>：项目使用<code>npm run build</code>打包之后，直接将dist目录里面的文件全部上传到服务器里面，然后在宝塔里面添加网站</p>
<p>例：myui.chengsong.info（创建站点时输入的网址）    然后将上传的dist目录与其相对应，可以修改配置文件</p>
<p><img src="https://gitee.com/cheng_song/picgo-img/raw/master/img/image-20201128164515904.png" alt="image-20201128164256348"></p>
<p>然后DNS也要添加解析到自己的服务器ip</p>
<img src="https://gitee.com/cheng_song/picgo-img/raw/master/img/image-20201128164256348.png" alt="image-20201128164515904" style="zoom:80%;" />



<img src="https://gitee.com/cheng_song/picgo-img/raw/master/img/image-20201223113254905.png" alt="image-20201223113254905" style="zoom:67%;" />



<p>后面基本就可以使用这个网站名进行访问了</p>
<h3 id="nuxt"><a href="#nuxt" class="headerlink" title="nuxt"></a>nuxt</h3><h4 id="服务器渲染应用部署"><a href="#服务器渲染应用部署" class="headerlink" title="服务器渲染应用部署"></a>服务器渲染应用部署</h4><p><a target="_blank" rel="noopener" href="https://www.aijiumai.cn/view/item/62.html">简单教程</a></p>
<p><a target="_blank" rel="noopener" href="https://blog.csdn.net/weixin_30646315/article/details/98028293">有些难看的教程</a></p>
<p>1.首先在宝塔面板里面添加一个站点，跟上面的vue部署差不多</p>
<p>2.后面再站点里面修改配置文件（注意端口号是在nuxt项目在package.json里面进行修改的）</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="string">&quot;name&quot;</span>: <span class="string">&quot;firstStudy&quot;</span>,</span><br><span class="line">  <span class="string">&quot;version&quot;</span>: <span class="string">&quot;1.0.0&quot;</span>,</span><br><span class="line">  <span class="string">&quot;private&quot;</span>: <span class="literal">true</span>,</span><br><span class="line">  <span class="string">&quot;scripts&quot;</span>: &#123;</span><br><span class="line">    <span class="string">&quot;dev&quot;</span>: <span class="string">&quot;nuxt&quot;</span>,</span><br><span class="line">    <span class="string">&quot;build&quot;</span>: <span class="string">&quot;nuxt build&quot;</span>,</span><br><span class="line">    <span class="string">&quot;start&quot;</span>: <span class="string">&quot;PORT=3003 nuxt start&quot;</span>, <span class="comment">//注意这里的port，在部署的时候会运行在这个端口上，主要是为了防止服务器端口冲突</span></span><br><span class="line">    <span class="string">&quot;generate&quot;</span>: <span class="string">&quot;nuxt generate&quot;</span>,</span><br><span class="line">    <span class="string">&quot;lint:js&quot;</span>: <span class="string">&quot;eslint --ext \&quot;.js,.vue\&quot; --ignore-path .gitignore .&quot;</span>,</span><br><span class="line">    <span class="string">&quot;lint&quot;</span>: <span class="string">&quot;yarn lint:js&quot;</span></span><br><span class="line">  &#125;,</span><br></pre></td></tr></table></figure>

<img src="C:\Users\admin\AppData\Roaming\Typora\typora-user-images\image-20210203180924769.png" style="zoom:80%;" />

<p>3.设置一个反向代理，主要是因为在服务器里面执行 npm run start 之后其实是在本地服务器的，需要代理到我们创建的站点上面</p>
<img src="https://gitee.com/cheng_song/picgo-img/raw/master/img/image-20210203181140550.png" alt="image-20210203181140550" style="zoom:80%;" />

<p>4.本地<code>npm run build</code>后，将build后的文件上传到服务器上面新建站点的对应目录中，有四个文件，具体的文件看上面的教程吧</p>
<p>5.在服务器上使用pm2开启进程守护</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line"><span class="comment">#在站点对应的目录里面执行</span></span><br><span class="line">pm2 start npm --name <span class="string">&quot;my-nuxt&quot;</span> -- run start  <span class="comment">#my-nuxt为我们自定义的项目名称，也是我们自定义的守护进程名称</span></span><br><span class="line"><span class="comment">#可使用 pm2 list 查看所有的pm2守护，也可直接使用宝塔面板自带的可视化pm2进行查看</span></span><br></pre></td></tr></table></figure>

<img src="https://gitee.com/cheng_song/picgo-img/raw/master/img/image-20210203185141914.png" alt="image-20210203185141914" style="zoom:80%;" />

<p>之后就能通过站点对应的域名进行访问了</p>
<h4 id="静态应用部署"><a href="#静态应用部署" class="headerlink" title="静态应用部署"></a>静态应用部署</h4><p>在nuxt项目里面执行</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">npm run generate</span><br></pre></td></tr></table></figure>

<p>之后将build出的dist目录上传到服务器的一个目录里，之后的步骤就跟上面的vue项目部署差不多了</p>
<h3 id="node"><a href="#node" class="headerlink" title="node"></a>node</h3><p>关于node的项目部署</p>
<p><a target="_blank" rel="noopener" href="https://blog.csdn.net/l_ppp/article/details/106085916">参考链接</a></p>
<p><a target="_blank" rel="noopener" href="https://www.bwmelon.com/archives/27/">node部署参考</a>：比较细节</p>
<p>将nodejs的文件除开node_module目录打包之后，上传到服务器，然后解压，使用pm2对nodejs解压目录进行链接（需要先在终端目录里面使用    <code>npm install</code> 安装依赖），这里的端口号是node项目app.js文件里面监听的端口号</p>
<img src="https://gitee.com/cheng_song/picgo-img/raw/master/img/image-20201128165455499.png" alt="image-20201128165455499" style="zoom:67%;" />

<p>需要将这个node项目映射到自己的服务器公网ip或者域名（域名需要在DNS里面添加解析），然后重启</p>
<p>添加了之后，就会在网站里面自动新建一个站点，在站点里面进行了反向代理，才能使得外部可访问</p>
<img src="https://gitee.com/cheng_song/picgo-img/raw/master/img/image-20210203190717110.png" alt="image-20210203190717110" style="zoom:80%;" />

<p>之后使用ip+端口号就能访问和请求得到数据，例：<a target="_blank" rel="noopener" href="http://115.159.103.48:5000/">http://115.159.103.48:5000/</a></p>
<p>或者域名访问    例：<a target="_blank" rel="noopener" href="http://api.chengsong.info/post?id=1">http://api.chengsong.info/post?id=1</a> 或  <a target="_blank" rel="noopener" href="http://api.chengsong.info:5000/post?id=1">http://api.chengsong.info:5000/post?id=1</a> </p>
<p>自己踩得坑：</p>
<p>上面的跨域问题的处理，需要解决。</p>
<p>然后一般会使用到数据库，如mysql，mongodb这些，需要将原来建的表和数据导入到宝塔自带的数据库里面，然后需要在node项目里面关于链接数据库的js文件里面修改数据库名和用户名才能使用</p>
<p><img src="https://gitee.com/cheng_song/picgo-img/raw/master/img/image-20201128165753322.png" alt="image-20201128165809874"></p>
<p><img src="https://gitee.com/cheng_song/picgo-img/raw/master/img/image-20201128165809874.png" alt="image-20201128165753322"></p>
<h3 id="域名相关"><a href="#域名相关" class="headerlink" title="域名相关"></a>域名相关</h3><p>对域名的SSL证书添加</p>
<p>我自己用的是腾讯云的域名，所以使用腾讯自己的免费SSL证书就行了</p>
<img src="https://gitee.com/cheng_song/picgo-img/raw/master/img/image-20210203191625079.png" alt="image-20210203191625079" style="zoom:80%;" />

<p>之后在宝塔面板里面对网站站点进行SSL证书申请和部署即可</p>
<img src="https://gitee.com/cheng_song/picgo-img/raw/master/img/image-20210203192016104.png" alt="image-20210203192016104" style="zoom:80%;" />

<p>后面就可通过<strong>https</strong>进行访问</p>
</div></article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">晟松</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://chengsong.info/2020/11/28/vue-nuxt-node部署问题/">https://chengsong.info/2020/11/28/vue-nuxt-node部署问题/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://chengsong.info">晟松</a>！</span></div></div><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/node-%E6%9C%8D%E5%8A%A1%E5%99%A8-%E9%83%A8%E7%BD%B2/">node 服务器 部署</a></div><nav id="pagination"><div class="prev-post pull-left"><a href="/2020/12/11/IDo-uniapp%E6%8A%80%E6%9C%AF%E9%97%AE%E9%A2%98%E9%9B%86%E5%90%88/"><i class="fa fa-chevron-left">  </i><span>IDo uniapp技术问题集合</span></a></div><div class="next-post pull-right"><a href="/2020/11/21/linux%E7%9F%A5%E8%AF%86%E7%82%B9%E5%A4%8D%E4%B9%A0/"><span>linux知识点复习</span><i class="fa fa-chevron-right"></i></a></div></nav></div></div><footer class="footer-bg" style="background-image: url(/img/blog-bg.jpg)"><div class="layout" id="footer"><div class="copyright">&copy;2020 - 2023 By 晟松</div><div class="framework-info"><span>驱动 - </span><a target="_blank" rel="noopener" href="http://hexo.io"><span>Hexo</span></a><span class="footer-separator">|</span><span>主题 - </span><a target="_blank" rel="noopener" href="https://github.com/Molunerfinn/hexo-theme-melody"><span>Melody</span></a></div><div class="icp"><a target="_blank" rel="noopener" href="http://www.beian.miit.gov.cn/"><span>湘ICP备2020021729号</span></a></div><div class="busuanzi"><script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><span id="busuanzi_container_page_pv"><i class="fa fa-file"></i><span id="busuanzi_value_page_pv"></span><span></span></span></div></div></footer><i class="fa fa-arrow-up" id="go-up" aria-hidden="true"></i><script src="https://cdn.jsdelivr.net/npm/animejs@latest/anime.min.js"></script><script src="https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-animate@latest/velocity.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-ui-pack@latest/velocity.ui.min.js"></script><script src="/js/utils.js?version=1.9.0"></script><script src="/js/fancybox.js?version=1.9.0"></script><script src="/js/sidebar.js?version=1.9.0"></script><script src="/js/copy.js?version=1.9.0"></script><script src="/js/fireworks.js?version=1.9.0"></script><script src="/js/transition.js?version=1.9.0"></script><script src="/js/scroll.js?version=1.9.0"></script><script src="/js/head.js?version=1.9.0"></script><script src="/js/search/local-search.js"></script><script>if(/Android|webOS|iPhone|iPod|iPad|BlackBerry/i.test(navigator.userAgent)) {
  $('#nav').addClass('is-mobile')
  $('footer').addClass('is-mobile')
  $('#top-container').addClass('is-mobile')
}</script><div class="search-dialog" id="local-search"><div class="search-dialog__title" id="local-search-title">本地搜索</div><div id="local-input-panel"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章"></div></div></div><hr><div id="local-search-results"><div id="local-hits"></div><div id="local-stats"><div class="local-search-stats__hr" id="hr"><span>由</span> <a target="_blank" rel="noopener" href="https://github.com/wzpan/hexo-generator-search" style="color:#49B1F5;">hexo-generator-search</a>
 <span>提供支持</span></div></div></div><span class="search-close-button"><i class="fa fa-times"></i></span></div><div class="search-mask"></div></body></html>